<?xml version="1.0" encoding="UTF-8"?>

<project name="TRIP" basedir="." default="go">

	<property name="name" value="TRIP"/>
	<property name="db.dir" value="db"/>
	<property name="src.dir" value="src"/>
  	<property name="build.dir" value="build"/>
	<property name="classes.dir" value="build/classes" />
	<property name="testreports.dir" value="reports"/>
	<property name="weblib.dir" value="WebContent/WEB-INF/lib"/>
	<property name="db.driver" value="com.mysql.jdbc.Driver"/>
	<property name="testdb.url" value="jdbc:mysql://localhost:3306/test"/>
	<property name="db.url" value="jdbc:mysql://localhost:3306/wtprac"/>
	<property name="db.user" value="root"/>
	<property name="db.pw" value="root"/>
	<property name="gf.dir" value="C:\glassfish3\glassfish\domains\domain1\autodeploy"/>
	
	<path id="build-classpath">
		<fileset dir="${weblib.dir}" includes="*.jar" />
		<fileset dir="${db.dir}" includes="*.jar" />
	</path>
	
	<path id="test-classpath">
			<pathelement location="${classes.dir}" />
			<path refid="build-classpath" />
	</path>
	
	<target name="dropDB">
		<echo message="Dropping DB using: ${db.driver} ${testdb.url}" />
		<sql driver="${db.driver}" url="${testdb.url}" userid="${db.user}" password="${db.pw}" onerror="continue">
			<classpath>
				<fileset dir="${db.dir}">
					<include name="mysql*.jar" />
				</fileset>
			</classpath>
			<transaction src="${db.dir}/mysql/DROP_DB.sql" />
		</sql>
	</target>
		
	<target name="createDB" depends="dropDB">
		<echo message="Creating DB using: ${db.driver} ${testdb.url}" />
		<sql driver="${db.driver}" url="${testdb.url}" userid="${db.user}" password="${db.pw}" onerror="continue">
			<classpath>
				<fileset dir="${db.dir}">
					<include name="mysql*.jar" />
				</fileset>
			</classpath>
			<transaction src="${db.dir}/mysql/CREATE_DB.sql" />
		</sql>
	</target>
		
	<target name="dropTables">
		<echo message="Dropping tables using: ${db.driver} ${db.url}" />
			<sql driver="${db.driver}" url="${db.url}" userid="${db.user}" password="${db.pw}" onerror="continue">
				<classpath>
					<fileset dir="${db.dir}">
						<include name="mysql*.jar" />
					</fileset>
				</classpath>
				<transaction src="${db.dir}/drop.sql" />
			</sql>
		</target>

	<target name="createTables" depends="createDB, dropTables">
		<echo message="Creating tables using: ${db.driver} ${db.url}" />
		<sql driver="${db.driver}" url="${db.url}" userid="${db.user}" password="${db.pw}" onerror="continue">
			<classpath>
				<fileset dir="${db.dir}">
					<include name="mysql*.jar" />
				</fileset>
			</classpath>
			<transaction src="${db.dir}/create.sql" />
		</sql>
	</target>

	<target name="emptyTables">
		<echo message="Emptying tables using: ${db.driver} ${db.url}" />
		<sql driver="${db.driver}" url="${db.url}" userid="${db.user}" password="${db.pw}">
			<classpath>
				<fileset dir="${db.dir}">
					<include name="mysql*.jar" />
				</fileset>
			</classpath>
			<transaction src="${db.dir}/delete.sql" />
		</sql>
	</target>

	<target name="populateTables" depends="createTables, emptyTables">
		<echo message="Populating tables using: ${db.driver} ${db.url}" />
		<sql driver="${db.driver}" url="${db.url}" userid="${db.user}" password="${db.pw}">
			<classpath>
				<fileset dir="${db.dir}">
					<include name="mysql*.jar" />
				</fileset>
			</classpath>
			<transaction src="${db.dir}/insert.sql" />
		</sql>
	</target>
  	
	<target name="build" depends="clean">
		<echo message="Compiling java files" />
		<mkdir dir="${build.dir}"/>
		<mkdir dir="${testreports.dir}"/>
		<mkdir dir="${classes.dir}" />
		
		<javac destdir="${classes.dir}" source="1.7" target="1.7" includeantruntime="false"
		  	   debug="true" deprecation="false" optimize="false" failonerror="true">
      		<src path="${src.dir}"/>
    		<classpath refid="build-classpath"/>
	    </javac>
		
  		<copy todir="${classes.dir}" preservelastmodified="true">
    		<fileset dir="${src.dir}/trip">
    			<include name="*.xml"/>
    		</fileset>
    	</copy>
    			
		<echo message="Building JAR file" />
		<jar jarfile="${build.dir}/${name}.jar">
			<manifest>
		    	<attribute name="Main-Class" value="trip.Main"/>
		    </manifest>
			<fileset dir="${classes.dir}">
				<include name="trip/*.*"/>
				<include name="dao/*.*"/>
				<include name="*.xml"/>
			</fileset>
		</jar>		    		
		  
	</target>    
	  
	
	<target name="run">
		<echo message="Running the program" />
		<java classname="trip.Main" fork="false">
			<classpath>
			    <path refid="build-classpath"/>
				<pathelement location="${build.dir}/${name}.jar"/>
			</classpath>
		</java>
	</target>
	
	<target name="clean">
		<echo message="Cleaning dirs" />
		<delete dir="${build.dir}" />
		<delete dir="${testreports.dir}"/>
		<delete file="${name}.war" />
		<delete dir="${gf.dir}" includes="*.war*"/>
	</target>
	
	<taskdef resource="testngtasks" classpath="${weblib.dir}/testng-6.8.jar"/>

	<target name="testng">
		<testng classpathref="test-classpath" outputDir="${testreports.dir}" verbose="2">
			<classfileset dir="${classes.dir}" includes="test/*.class" />
		</testng>
	</target>

	<target name="war" depends="build, testng, startserver" >
		<echo message="Building WAR file" />
		<war destfile="${name}.war" index="true" webxml="WebContent/WEB-INF/web.xml">
			<classes dir="${classes.dir}" />
			<lib dir="${weblib.dir}" />
			<lib dir="${db.dir}" includes="*.jar" />
			<fileset dir="WebContent">
				<include name="**/*.*" />
			</fileset>
		</war>
		
		<copy todir="${gf.dir}" file="${name}.war" preservelastmodified="true"/>
	</target>
	
	<target name="stopserver">
		<echo message="Stoping the server" />
		<exec executable="C:\glassfish3\glassfish\bin\asadmin.bat">
			<arg value="stop-domain" />
			<arg value="domain1" />
		</exec>
	</target>
	
	<target name="startserver" depends="stopserver">
		<echo message="Starting the server" />
		<exec executable="C:\glassfish3\glassfish\bin\asadmin.bat">
			<arg value="start-domain" />
			<arg value="domain1" />
		</exec>
	</target>
	
	<target name="go" depends="populateTables, war">
		<echo message="Deploying WAR file" />
		<waitfor maxwait="30" maxwaitunit="second">
		        <available file="${gf.dir}/${name}.war_deployed"/>
		</waitfor>
		<echo message="Opening Browser" />
		<exec executable="C:\Program Files (x86)\Mozilla Firefox\firefox.exe">
		    <arg value="http://localhost:8080/TRIP/"/>
		</exec>
	</target>	
	
</project>
